package bean;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class JudgeBean {
    public ArrayList<Judge> adminArrayList;
    public HashMap<String,String> adminMap;
    public Connection connection;
    public PreparedStatement preparedStatement;
    public Statement statement;
    public ResultSet resultSet;

    public JudgeBean() {
        this.adminArrayList = new ArrayList<>();
        this.adminMap = new HashMap<>();
    }

    public ArrayList<Judge> getJudgeArrayList() {
        return adminArrayList;
    }

    public HashMap<String, String> getJudgeMap() {
        return adminMap;
    }

    public void link() {
        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection connection=DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void loadJudge(){
        String sql="select * from Judge";
        try {
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            while(resultSet.next()){
                Judge admin=new Judge(resultSet.getString(1),resultSet.getString(2));
                adminArrayList.add(admin);
                adminMap.put(resultSet.getString(1),resultSet.getString(2));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public void insertJudge(Judge admin){
        adminArrayList.add(admin);
        adminMap.put(admin.getId(),admin.getReferee());
        String sql="insert into Judge values(?,?)";
        try {
            preparedStatement=connection.prepareStatement(sql);

            preparedStatement.setString(1,admin.getReferee());
            preparedStatement.setString(2,admin.getId());
            preparedStatement.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
